﻿@using Ext.Net.MVC
@using Ext.Net
@model IEnumerable<SocialGame.Models.QuestionTypeModel>
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_BaseLayout.cshtml";
}
@section headtag
{
    <script>
        var updateRecord = function (form) {
            if (form.getForm()._record == null) {
                return;
            }

            if (!form.getForm().isValid()) {
                Ext.net.Notification.show({
                    iconCls: "icon-exclamation",
                    html: "Form is invalid",
                    title: "Error"
                });
                return false;
            }

            form.getForm().updateRecord();
        };

        var addRecord = function (form, grid) {
            if (!form.getForm().isValid()) {
                Ext.net.Notification.show({
                    iconCls: "icon-exclamation",
                    html: "Form is invalid",
                    title: "Error"
                });

                return false;
            }

            grid.store.insert(0, new QuestionType(form.getForm().getValues()));
            form.getForm().reset();
        };

        var onStoreException = function (propxy, response, operation) {
            var error = operation.getError(),
                        message = Ext.isString(error) ? error : ('(' + error.status + ')' + error.statusText);

            Ext.net.Notification.show({
                iconCls: 'icon-exclamation',
                html: message,
                title: 'EXCEPTION',
                autoScroll: true,
                hideDelay: 5000,
                width: 300,
                height: 200
            });
        };
    </script>
}
@section content
{
    @(
 Html.X().FormPanelForModel()
            .ID("UserForm")
            .Icon(Icon.Help)
            .Frame(true)
            .Title("Câu hỏi -- Nhập thông tin các trường dữ liệu")
            .Width(500)
            .DefaultAnchor("100%")
            .FieldDefaults(d =>
            {
                d.LabelAlign = LabelAlign.Right;
            })
            .Buttons(
                Html.X().Button()
                    .Text("Save")
                    .Icon(Icon.Disk)
                    .Handler("updateRecord(this.up('form'));"),

                Html.X().Button()
                    .Text("Create")
                    .Icon(Icon.UserAdd)
                    .Handler("addRecord(this.up('form'), App.GridPanel1);"),

                Html.X().Button()
                    .Text("Reset")
                    .Handler("this.up('form').getForm().reset();")
            )
    )
    @(    
 Html.X().GridPanel()
            .ID("GridPanel1")
            .Store(
                Html.X().StoreForModel()
                    .AutoSync(true)
                    .ShowWarningOnFailure(false)
                    .Listeners(l =>
                    {
                        l.Exception.Fn = "onStoreException";
                    })
                    .SyncUrl(Url.Action("HandleChanges"))
            )
            .Icon(Icon.Help)
            .Frame(true)
            .Title("Users")
            .Height(400)
            .Width(500)
            .StyleSpec("margin-top: 10px;")
            .ColumnModel(           
                Html.X().ColumnFor(Model, m => m.QuestionType)
                            .ToBuilder<Column.Builder>().Flex(1).Text("Mã câu hỏi").Editor(
                                Html.X().TextField().AllowBlank(false)
                            ),

                Html.X().ColumnFor(Model, m => m.QuestionName)
                    .ToBuilder<Column.Builder>().Text("Loại câu hỏi")
                    .Flex(1)
                    .Editor(
                        Html.X().TextField().AllowBlank(false)
                    ),
               Html.X().CommandColumn()
                   .Width(70)
                   .Commands(
                        Html.X().GridCommand()
                            .Text("Reject")
                            .ToolTip(t =>
                            {
                                t.Text = "Reject row changes";
                            })
                            .CommandName("reject")
                            .Icon(Icon.ArrowUndo)
                   )
                   .PrepareToolbar(t =>
                   {
                       t.Handler = "toolbar.items.get(0).setVisible(record.dirty);";
                   })
                   .Listeners(l =>
                   {
                       l.Command.Handler = "record.reject();";
                   })
            )
            .TopBar(
                Html.X().Toolbar()
                    .Items(
                        Html.X().Button()
                            .Text("Add")
                            .Icon(Icon.Add)
                            .Handler("this.up('grid').store.insert(0, new QuestionType());"),

                        Html.X().Button()
                            .Text("Delete")
                            .Icon(Icon.Exclamation)
                            .Handler("this.up('grid').deleteSelected(); App.UserForm.getForm().reset();"),

                        Html.X().ToolbarSeparator(),

                        Html.X().Button()
                            .Text("Auto Sync")
                            .EnableToggle(true).Hidden(true)
                            .Pressed(true)
                            .ToolTip("When enabled, Store will execute Ajax requests as soon as a Record becomes dirty.")
                            .ToggleHandler("function(b, pressed){this.up('grid').store.autoSync = pressed;}")
                    )
            )
            .SelectionModel(
                Html.X().RowSelectionModel()
                    .Mode(SelectionMode.Single)
                    .Listeners(l =>
                    {
                        l.Select.Handler = "App.UserForm.getForm().loadRecord(record);";
                    })
            )
            .Buttons(
                Html.X().Button()
                    .Text("Save")
                    .Icon(Icon.Disk)
                    .Handler("this.up('grid').store.sync();")
            )
            .Plugins(
                Html.X().CellEditing()
            )
    )